package com.thirteenClassicsServer.controller;

import cn.hutool.core.bean.BeanUtil;
import com.thirteenClassics.dto.*;
import com.thirteenClassics.entity.CatalogBook;
import com.thirteenClassics.entity.Sentence;
import com.thirteenClassics.enumeration.ActionEnum;
import com.thirteenClassics.result.PageResult;
import com.thirteenClassics.result.Result;
import com.thirteenClassics.vo.MarkCatalogQueryJdbVO;
import com.thirteenClassics.vo.MarkCatalogQuerySingleVO;
import com.thirteenClassicsServer.common.CommonService;
import com.thirteenClassicsServer.mapper.CatalogBookMapper;
import com.thirteenClassicsServer.mapper.SentenceMapper;
import com.thirteenClassicsServer.service.IBookService;
import com.thirteenClassicsServer.service.IEntryService;
import com.thirteenClassicsServer.service.IMarkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("/mark")
public class MarkController {
    @Resource
    private IBookService bookService;
    @Resource
    private IMarkService markService;
    @Resource
    private IEntryService entryService;
    @Resource
    private CatalogBookMapper catalogBookMapper;
    @Resource
    private SentenceMapper sentenceMapper;
    @Resource
    private CommonService commonService;


    @GetMapping("/jdbList")
    public Result<PageResult> selectMarkJdbBook(EntryPageQueryDTO entryPageQueryDTO) {
        log.info("经典版分页查询：{}", entryPageQueryDTO);
        PageResult pageResult = markService.pageQueryMarkJdb(entryPageQueryDTO);
        return Result.success(pageResult);
    }

    @GetMapping()
    public Result<PageResult> selectMarkBook(EntryPageQueryDTO entryPageQueryDTO) {
        log.info("少语版分页查询：{}", entryPageQueryDTO);
        PageResult pageResult = markService.pageQueryMark(entryPageQueryDTO);
        return Result.success(pageResult);
    }

    @GetMapping("/contentJdb")
    public Result<MarkCatalogQueryJdbVO> selectContentBookJdb(MarkCatalogQueryDTO markCatalogQueryDTO) {
        log.info("查询当前经典版页的内容：{}", markCatalogQueryDTO);

        MarkCatalogQueryJdbVO MarkCatalogQueryVO = markService.selectCatalogAndAll(markCatalogQueryDTO);

        log.info("当前经典版页内容的查询结果：{}{}{}{}{}{}",
                MarkCatalogQueryVO.getName() == null ? "" : "name=" + MarkCatalogQueryVO.getName() + ",",
                MarkCatalogQueryVO.getCatalogId() == null ? "" : "sentence=" + MarkCatalogQueryVO.getCatalogId() + ",",
                MarkCatalogQueryVO.getSentence() == null ? "" : "sentence=" + MarkCatalogQueryVO.getSentence() + ",",
                MarkCatalogQueryVO.getExplainS() == null ? "" : "explainS=" + MarkCatalogQueryVO.getExplainS() + ",",
                MarkCatalogQueryVO.getFc() == null ? "" : "fc=" + MarkCatalogQueryVO.getFc() + ",",
                MarkCatalogQueryVO.getEntities() == null ? "" : "Entities=" + MarkCatalogQueryVO.getEntities());

        EntryCatalogQueryDTO entryCatalogQueryDTO = new EntryCatalogQueryDTO();
        BeanUtil.copyProperties(markCatalogQueryDTO, entryCatalogQueryDTO);
        entryService.saveOperationHistory(entryCatalogQueryDTO, ActionEnum.MARKACTION.getCode());

        return Result.success(MarkCatalogQueryVO);
    }

    @PostMapping("/jdbSentenceTotal")
    public Result<Integer> queryJdbSentenceTotal (@RequestBody QuerySentenceTotalDTO querySentenceTotalDTO){
        log.info("查询当前章节的所有句子数：{}", querySentenceTotalDTO);
        CatalogQueryDTO catalogQueryDTO = new CatalogQueryDTO();
        BeanUtils.copyProperties(querySentenceTotalDTO, catalogQueryDTO);
        CatalogBook catalogBook = commonService.getJdbCatalogBookByBookAndIndex(catalogQueryDTO);
        Sentence sentence = new Sentence();
        sentence.setEntryId(catalogBook.getId());
        return Result.success(sentenceMapper.selectSentenceSumOnlyJdb(sentence));

    }

    @PostMapping("/saveJdb")
    public Result save(@RequestBody MarkJdbDto markJdbDto) {
        log.info("经典版标注句子信息：{}", markJdbDto);
        markService.saveJdb(markJdbDto);
        return Result.success("saveJdb success!");
    }


    @GetMapping("/contentComp")
    public Result<MarkCatalogQuerySingleVO> selectContentComp(MarkCatalogQueryDTO markCatalogQueryDTO) {
        log.info("查询当前对照版页的内容：{}", markCatalogQueryDTO);

        MarkCatalogQuerySingleVO markCatalogQuerySingleVO = markService.selectCatalogAndAllComp(markCatalogQueryDTO);

        EntryCatalogQueryDTO entryCatalogQueryDTO = new EntryCatalogQueryDTO();
        BeanUtil.copyProperties(markCatalogQueryDTO, entryCatalogQueryDTO);
        entryService.saveOperationHistory(entryCatalogQueryDTO, ActionEnum.MARKACTION.getCode());

        log.info("当前对照版页内容的查询结果（仅显示少语对照内容）：{}{}{}{}{}{}{}{}",
                markCatalogQuerySingleVO.getName() == null ? "" : "name=" + markCatalogQuerySingleVO.getName() + ",",
                markCatalogQuerySingleVO.getCatalogId() == null ? "" : "catalogId=" + markCatalogQuerySingleVO.getCatalogId() + ",",
                markCatalogQuerySingleVO.getSentencedz() == null ? "" : "sentencedz=" + markCatalogQuerySingleVO.getSentencedz() + ",",
                markCatalogQuerySingleVO.getManwengld() == null ? "" : "manwengld=" + markCatalogQuerySingleVO.getManwengld() + ",",
                markCatalogQuerySingleVO.getSentence1() == null ? "" : "sentence1=" + markCatalogQuerySingleVO.getSentence1() + ",",
                markCatalogQuerySingleVO.getFc1() == null ? "" : "fc1=" + markCatalogQuerySingleVO.getFc1() + ",",
                markCatalogQuerySingleVO.getEntities1() == null ? "" : "entities1=" + markCatalogQuerySingleVO.getEntities1() + ",",
                markCatalogQuerySingleVO.getDictionaries() == null ? "" : "dictionaries=" + markCatalogQuerySingleVO.getDictionaries());

        return Result.success(markCatalogQuerySingleVO);
    }

    @PostMapping("/saveComp")
    public Result saveComp(@RequestBody MarkCompDto markCompDto) {
        log.info("对照版标注句子信息：{}", markCompDto);
        markService.saveComp(markCompDto);
        return Result.success("saveComp success!");
    }

}
